package edu.hawaii.stack.page;

import static org.junit.Assert.assertEquals;
import org.apache.wicket.model.Model;
import org.apache.wicket.util.tester.FormTester;
import org.apache.wicket.util.tester.WicketTester;
import org.junit.Test;

/**
 * Tests the stack page.
 * 
 * @author Aric West
 */
public class TestStackPage {
  
  /**
   * 
   */
  @Test
  public void testStackPage() {
    // Constant literal strings on advice of PMD.
    final String INPUT_FIELD = "inputField";     
    final String PUSH_BUTTON = "pushButton";
    final String POP_BUTTON = "popButton";
    final String STACK_FORM = "stackForm";
    
    // Start up the WicketTester and check that the startpage renders. 
    WicketTester tester = new WicketTester(new StackApplication());
    tester.startPage(StackPage.class);
    tester.assertRenderedPage(StackPage.class);
   
    FormTester stackFormTester = tester.newFormTester(STACK_FORM);
  
    String inputString = stackFormTester.getTextComponentValue(INPUT_FIELD);
    assertEquals("Testing text field", "", inputString);
    
    stackFormTester.setValue(INPUT_FIELD, "First push");
    stackFormTester.submit(PUSH_BUTTON);
    inputString = stackFormTester.getTextComponentValue(INPUT_FIELD);
    assertEquals("Testing text field clears on push", "", inputString);

    Model rowModel = (Model) tester.getComponentFromLastRenderedPage("stackTable:1").getModel();
    String element = (String) rowModel.getObject();
    assertEquals("Testing stack table", "First push", element);
    
    // Need a new FormTester after each submit.  Wow that's irritating.
    stackFormTester = tester.newFormTester(STACK_FORM);
    stackFormTester.setValue(INPUT_FIELD, "Second push");
    stackFormTester.submit(PUSH_BUTTON);
    
    stackFormTester = tester.newFormTester(STACK_FORM);
    stackFormTester.setValue(INPUT_FIELD, "Third push");
    stackFormTester.submit(PUSH_BUTTON);
    
    stackFormTester = tester.newFormTester(STACK_FORM);
    stackFormTester.setValue(INPUT_FIELD, "Fourth push");
    stackFormTester.submit(PUSH_BUTTON);
    
    rowModel = (Model) tester.getComponentFromLastRenderedPage("stackTable:1").getModel();
    element = (String) rowModel.getObject();
    assertEquals("Testing stack table", "Fourth push", element);
    rowModel = (Model) tester.getComponentFromLastRenderedPage("stackTable:4").getModel();
    element = (String) rowModel.getObject();
    assertEquals("Testing stack table", "First push", element);
        
    stackFormTester = tester.newFormTester(STACK_FORM);
    stackFormTester.submit(POP_BUTTON);
    
    stackFormTester = tester.newFormTester(STACK_FORM);
    stackFormTester.submit(POP_BUTTON);
    inputString = stackFormTester.getTextComponentValue(INPUT_FIELD);
    assertEquals("Testing text field on pop", "Third push", inputString);
    
    stackFormTester = tester.newFormTester(STACK_FORM);
    stackFormTester.submit("clearButton");
    inputString = stackFormTester.getTextComponentValue(INPUT_FIELD);
    assertEquals("Testing text field on clear", "", inputString);
    
    stackFormTester = tester.newFormTester(STACK_FORM);
    stackFormTester.submit(POP_BUTTON);
    inputString = stackFormTester.getTextComponentValue(INPUT_FIELD);
    assertEquals("Testing empty pop", "No element to pop.", inputString);
    
    stackFormTester = tester.newFormTester(STACK_FORM);
    stackFormTester.setValue(INPUT_FIELD, "");
    stackFormTester.submit(PUSH_BUTTON);
    inputString = stackFormTester.getTextComponentValue(INPUT_FIELD);
    assertEquals("Testing empty push", "Cannot push an empty field.", inputString);
  }
}
